home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / MACVOGL- / TEXT.C < prev    next >
C/C++ Source or Header  |  1992-07-19  |  4KB  |  261 lines

  1. #include <stdio.h>
  2.  
  3. #ifdef PC
  4. #include <string.h>
  5. #else
  6. #include <string.h>
  7. #endif
  8. #include "vogl.h"
  9.  
  10. static    double    c_x, c_y, c_z;    /* Current character position */
  11.  
  12. double    getcharwidth();
  13.  
  14. /*
  15.  * font
  16.  *     assigns a font.
  17.  */
  18. void
  19. font(id)
  20.     short    id;
  21. {
  22.     Token    *tok;
  23.  
  24.     if (!vdevice.initialised)
  25.         verror("font: vogl not initialised");
  26.  
  27.     if (id < 0 || id >= vdevice.maxfontnum)
  28.         verror("font: font number is out of range");
  29.     
  30.     if (vdevice.inobject) {
  31.         tok = newtokens(2);
  32.         tok[0].i = FONT;
  33.         tok[1].i = id;
  34.         return;
  35.     }
  36.  
  37.     if (id == 1) {
  38.         if (!(*vdevice.dev.Vfont)(vdevice.dev.large)) 
  39.             verror("font: unable to open large font");
  40.     } else if (id == 0) {
  41.         if (!(*vdevice.dev.Vfont)(vdevice.dev.small))
  42.             verror("font: unable to open small font");
  43.     }
  44.  
  45.     vdevice.attr->a.fontnum = id;
  46. }
  47.  
  48. /*
  49.  * getcharwidth
  50.  *
  51.  *     Gets the width of one char on world coords.
  52.  */
  53. static double    
  54. getcharwidth()
  55. {
  56.     double           a, b, c, d;
  57.  
  58.     VtoWxy(vdevice.hwidth, vdevice.hheight, &c, &d);
  59.     VtoWxy(0.0, 0.0, &a, &b);
  60.     c -= a;
  61.     return(c);
  62. }
  63.  
  64. /*
  65.  * charstr
  66.  *
  67.  * Draw a string from the current character position.
  68.  *
  69.  */
  70. void
  71. charstr(str)
  72.     char     *str;
  73. {
  74.     double    cx, cy, cz;
  75.     char    c;
  76.     Token    *tok;
  77.     int    oldclipoff = vdevice.clipoff;
  78.     int    sl = strlen(str);
  79.  
  80.     if(!vdevice.initialised) 
  81.         verror("charstr: vogl not initialized");
  82.  
  83.     if (vdevice.inobject) {
  84.         tok = newtokens(2 + strlen(str) / sizeof(Token));
  85.  
  86.         tok[0].i = DRAWSTR;
  87.         strcpy((char *)&tok[1], str);
  88.  
  89.         return;
  90.     }
  91.  
  92.     cx = vdevice.cpW[V_X];
  93.     cy = vdevice.cpW[V_Y];
  94.     cz = vdevice.cpW[V_Z];
  95.  
  96.     vdevice.clipoff = 1;    /* Forces update of device coords */
  97.     move(c_x, c_y, c_z);    /* Updates device coords      */
  98.  
  99.     /*   If not clipping then simply display text and return  */
  100.  
  101.     if (oldclipoff) {
  102.         (*vdevice.dev.Vstring)(str);
  103.     } else { /* Check if string is within viewport */
  104.         int    left_s = vdevice.cpVx;
  105.         int    bottom_s = vdevice.cpVy - (int)vdevice.hheight;
  106.         int    top_s = bottom_s + (int)vdevice.hheight;
  107.         int    right_s = left_s + (int)((sl + 1) * vdevice.hwidth);
  108.  
  109.         if (left_s > vdevice.minVx &&
  110.             bottom_s < vdevice.maxVy &&
  111.             top_s > vdevice.minVy &&
  112.             right_s < vdevice.maxVx) {
  113.             (*vdevice.dev.Vstring)(str);
  114.         } else {
  115.             while (c = *str++) {
  116.                 if (vdevice.cpVx > vdevice.minVx &&
  117.                     vdevice.cpVx < vdevice.maxVx - (int)vdevice.hwidth) {
  118.                     (*vdevice.dev.Vchar)(c);
  119.                 }
  120.                 vdevice.cpVx += vdevice.hwidth;
  121.             }
  122.         }
  123.     }
  124.  
  125.     c_x += getcharwidth() * sl;
  126.  
  127.     move(cx, cy, cz);
  128.     vdevice.clipoff = oldclipoff;
  129. }
  130.  
  131. /*
  132.  * cmov
  133.  *
  134.  *    Sets the current character position.
  135.  */
  136. void
  137. cmov(x, y, z)
  138.     double    x, y, z;
  139. {
  140.     Token    *tok;
  141.  
  142.     if (vdevice.inobject) {
  143.         tok = newtokens(4);
  144.  
  145.         tok[0].i = CMOV;
  146.         tok[1].f = x;
  147.         tok[2].f = y;
  148.         tok[3].f = z;
  149.  
  150.         return;
  151.     }
  152.  
  153.     c_x = x;
  154.     c_y = y;
  155.     c_z = z;
  156. }
  157.  
  158.  
  159. /*
  160.  * cmov2
  161.  *
  162.  *    Sets the current character position. Ignores the Z coord.
  163.  *    
  164.  *
  165.  */
  166. void
  167. cmov2(x, y)
  168.     double    x, y;
  169. {
  170.     c_x = x;
  171.     c_y = y;
  172.     c_z = 0.0;
  173. }
  174.  
  175. /*
  176.  * cmovi
  177.  *
  178.  *    Same as cmov but with integer arguments....
  179.  */
  180. void
  181. cmovi(x, y, z)
  182.     Icoord    x, y, z;
  183. {
  184.     c_x = (Coord)x;
  185.     c_y = (Coord)y;
  186.     c_z = (Coord)z;
  187. }
  188.  
  189. /*
  190.  * cmovs
  191.  *
  192.  *    Same as cmov but with short integer arguments....
  193.  */
  194. void
  195. cmovs(x, y, z)
  196.     Scoord    x, y, z;
  197. {
  198.     c_x = (Coord)x;
  199.     c_y = (Coord)y;
  200.     c_z = (Coord)z;
  201. }
  202.  
  203. /*
  204.  * cmov2i
  205.  *
  206.  *    Same as cmov2 but with integer arguments....
  207.  */
  208. void
  209. cmov2i(x, y)
  210.     Icoord    x, y;
  211. {
  212.     c_x = (Coord)x;
  213.     c_y = (Coord)y;
  214.     c_z = 0.0;
  215. }
  216.  
  217. /*
  218.  * cmov2s
  219.  *
  220.  *    Same as cmov but with short integer arguments....
  221.  */
  222. void
  223. cmov2s(x, y)
  224.     Scoord    x, y;
  225. {
  226.     c_x = (Coord)x;
  227.     c_y = (Coord)y;
  228.     c_z = 0.0;
  229. }
  230.  
  231. /*
  232.  * getwidth
  233.  *
  234.  * Return the maximum Width of the current font.
  235.  *
  236.  */
  237. long
  238. getwidth()
  239. {
  240.     if (!vdevice.initialised)
  241.         verror("getwidth: vogl not initialised");
  242.  
  243.  
  244.     return((long)vdevice.hwidth);
  245. }
  246.  
  247. /* 
  248.  * getheight
  249.  *
  250.  * Return the maximum Height of the current font
  251.  */
  252. long 
  253. getheight()
  254. {
  255.     if (!vdevice.initialised)
  256.         verror("getheight: vogl not initialized");
  257.  
  258.     return((long)vdevice.hheight);
  259. }
  260.  
  261.